package com.feifan.monthhelper.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feifan.monthhelper.R;
import com.feifan.monthhelper.bean.db.DayRecordBean;
import com.feifan.monthhelper.calendar.CalendarUtils;
import com.feifan.monthhelper.constant.Constant;
import com.feifan.monthhelper.db.DBUtls;
import com.feifan.monthhelper.ui.adapter.LoveRecordAdapter;
import com.feifan.monthhelper.ui.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 月经周期记录
 */
public class LoveRecordActivity extends BaseActivity {

    @BindView(R.id.rl_title)
    View rlTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.refresh_header)
    ClassicsHeader refreshHeader;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.ll_empty_view)
    LinearLayout llEmptyView;
    @BindView(R.id.empty_view)
    RelativeLayout emptyView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private LoveRecordAdapter adapter;
    private int page = 0;//分页从0开始

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View getTitleView() {
        return rlTitle;
    }

    @Override
    protected void initData() {
//        loveRecordBeans = getIntent().getParcelableArrayListExtra(Constant.ConstantName.LAST_30_LOVES);
        adapter = new LoveRecordAdapter();
    }

    @Override
    protected void initView() {
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(adapter);
//        adapter.replace(loveRecordBeans);
        List<DayRecordBean> recordBeans = DBUtls.loadLoveBy(page);
        adapter.add(recordBeans);

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                loadData();
            }
        });

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void loadData() {
        Disposable disposable = Observable.create((ObservableOnSubscribe<List<DayRecordBean>>) emitter -> {
            emitter.onNext(DBUtls.loadLoveBy(page));
            emitter.onComplete();
        })
                .doOnSubscribe(disposable1 -> showLoadingDialog("初始化数据..."))
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnError(o -> dismisDialog())
                .doOnComplete(() -> dismisDialog())
                .subscribe(o -> {
                    List<DayRecordBean> recordBeans = o;
                    updateView(recordBeans);
                });
        addDisposable(disposable);
//        List<DayRecordBean> recordBeans = DBUtls.loadLoveBy(page);
//        if(recordBeans.size() < Constant.PAGE_COUNT) {
//            refreshLayout.setEnableLoadMore(false);
//        } else {
//            refreshLayout.setEnableLoadMore(true);
//        }
//        if(page == 0) {
//            adapter.replace(recordBeans);
//            refreshLayout.finishRefresh();
//        } else {
//            adapter.add(recordBeans);
//            refreshLayout.finishLoadMore();
//        }
    }

    private void updateView(List<DayRecordBean> recordBeans) {
        if (recordBeans.size() < Constant.PAGE_COUNT) {
            refreshLayout.setEnableLoadMore(false);
        } else {
            refreshLayout.setEnableLoadMore(true);
        }
        if (page == 0) {
            adapter.replace(recordBeans);
            refreshLayout.finishRefresh();
        } else {
            adapter.add(recordBeans);
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    protected int setContentLayout() {
        return R.layout.activity_love_records;
    }
}
